﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Core.Enums;
using Company.Core.Interfaces;
using Company.Database.Entities;
using Company.Shell.Dialogs;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.Services;
using HandyControl.Controls;
using System.Windows.Input;


namespace Company.Shell.ViewModels
{
    public class ConfigViewModel: ObservableObject
    {
        public ICommand? SaveCommand { get;  }
        public ICommand? AddAlarmItemCommand { get; }
        private IConfigManager ConfigManager { get; }
        public IConfigProvider ConfigProvider { get; }

        public IRoomManager RoomManager { get; }
        public Config Config { get; }
        private AlarmItem alarmItem ;
        public AlarmItem AlarmItem
        {
            get => alarmItem;
            set => SetProperty(ref alarmItem, value);
        }

        public ConfigViewModel(
            IConfigManager ConfigManager,
            IConfigManager configManager, 
            IConfigProvider configProvider, 
            IRoomManager roomManager)
        {
            
            RoomManager = roomManager;
            ConfigManager = configManager;
            ConfigProvider = configProvider;
            Config = configProvider.Config;
            SaveCommand = new RelayCommand(OnSaveCommand);
            AddAlarmItemCommand = new RelayCommand(OnAddAlarmItemCommand);

            System.Diagnostics.Debug.WriteLine($"ConfigViewModel initialized. RoomCount: {Config.SoftwareConfig.RoomCount}");

        }

        private void OnAddAlarmItemCommand()
        {
            if (string.IsNullOrEmpty(AlarmItem.Address))
            {
                Dialog.Show(new MessageDialog("请输出报警地址和位"));
                return;
            }
            if (string.IsNullOrEmpty(AlarmItem.Description))
            {
                Dialog.Show(new MessageDialog("请输出报警描叙"));
                return;
            }
            ConfigProvider.AlarmItems.Add(AlarmItem);
            AlarmItem = new AlarmItem();
        }

        private void OnSaveCommand()
        {
            ConfigManager.Write(ConfigType.System, Config);//保存配置
            ConfigManager.Write(ConfigType.Alarm, ConfigProvider.AlarmItems);//保存报警配置
            Dialog.Show<MessageDialog>();
        }
    }
}
